package com.zwy.leetCode;

import java.util.*;

public class Day12Test {
    /**
     * 1423. 可获得的最大点数
     * @param cardPoints
     * @param k
     * @return
     */
    public int maxScore(int[] cardPoints, int k) {
       int []dp=new int[k+1];
        int []dp2=new int[k+1];
        int max=0;
       dp[0]=0;
       dp2[0]=0;

        for (int i = 1; i <= k; i++) {
            dp[i]=dp[i-1]+cardPoints[i-1];
            dp2[i]=dp2[i-1]+cardPoints[cardPoints.length-i];
        }
        for (int i = 0; i <=k ; i++) {
            max=Math.max(max,dp[i]+dp2[k-i]);
        }
        return max;
    }


    /**
     *
     */
    Map<Integer,Set<Integer>>map= new HashMap<>();
    int pnum=0;
    int knum=0;
    public int subarraysWithKDistinct(int[] nums, int k) {
        int length=nums.length;
        int count=0;
        int step=k;
        int []minNum=new int[nums.length];
        while(step<=length){
            for (int i = 0; i < nums.length; i++) {
                int c=0;
                    if (minNum[i] <= k)
                        c = hasDiffrenNum(nums, i, i + step,step==k);
                if(c==k){
                    count++;
                }
                if(c>=minNum[i])minNum[i]=c;
            }
            step++;
        }
        System.out.println(pnum);
        System.out.println(knum);
        return count;
    }
    public int hasDiffrenNum(int []arr,int start,int end,boolean flag){
        int count=0;
        if(!flag) {
            pnum++;
            if (end > arr.length) return end - start + 10;
            if (map.get(start).contains(arr[end - 1])) {
                return map.get(start).size();
            } else {
                Set<Integer> set = map.get(start);
                set.add(arr[end - 1]);
                map.put(start, set);
                return map.get(start).size();
            }
        }
       else {
           knum++;
           Set<Integer> set=new HashSet();
            for (int i = start; i < end && start <= arr.length - end + start; i++) {
                if (!set.contains(arr[i])) {
                    count++;
                    if (count > end - start) break;
                    set.add(arr[i]);
                }
            }
            map.put(start,set);
            return map.get(start).size();
        }
    }

    public static void main(String[] args) {
        Day12Test test=new Day12Test();
        int []cardPoints = {1,2,1,2,3};
        int k = 2;
        test.subarraysWithKDistinct(cardPoints,k);
    }
}
